import pygame

from ball import Ball
from player import Player
from bricks import Bricks

from pygame.locals import *

class World(object):
    def __init__(self):       
        #create background surface
        self.background = pygame.Surface(pygame.display.get_surface().get_size())
        self.background = self.background.convert()
        self.background.fill((40, 40, 40))
        
        self.hud_font = pygame.font.Font(None, 48)
        self.hud_smallfont = pygame.font.Font(None, 30)
        
        self.closed = False
        
        self.new_game()
        
    def new_game(self):
        self.closed = False
    
        self.player = Player()
        self.bricks = Bricks()
        
        #create ball at center of paddle
        ball_pos_x = self.player.get_x() + self.player.get_width() / 2
        ball_pos_y = self.player.get_y() - 20 #small y offset so it's not right on the paddle
        self.ball = Ball(ball_pos_x, ball_pos_y)
        
        self.lives = 3
        self.score = 0
        
    def get_bricks(self):
        return self.bricks
        
    def get_player(self):
        return self.player
        
    def lose_life(self):
        self.lives -= 1
        
    def add_score(self, amount):
        self.score += amount
        
    def draw(self, screen):
        screen.blit(self.background, (0, 0))
    
        self.bricks.draw(screen)
        self.player.draw(screen)
        self.ball.draw(screen)
        
        #draw HUD
        text_padding = 24
        text_shadow_offset = 4
        
        lives_text = "Lives: " + str(self.lives)
        lives_display = self.hud_font.render(lives_text, True, (220, 220, 220))
        lives_display_shadow = self.hud_font.render(lives_text, False, (20, 20, 20))
        lives_x = text_padding
        lives_y = screen.get_height() - (text_padding + lives_display.get_height())
        screen.blit(lives_display_shadow, (lives_x + text_shadow_offset, lives_y + text_shadow_offset))
        screen.blit(lives_display, (lives_x, lives_y))
        
        score_text = "Score: " + str(self.score)
        score_display = self.hud_font.render(score_text, True, (220, 220, 220))
        score_display_shadow = self.hud_font.render(score_text, False, (20, 20, 20))
        score_x = screen.get_width() - (text_padding + score_display.get_width())
        score_y = screen.get_height() - (text_padding + score_display.get_height())
        screen.blit(score_display_shadow, (score_x + text_shadow_offset, score_y + text_shadow_offset))
        screen.blit(score_display, (score_x, score_y))
        
        speed_text = "BONUS " +str(self.ball.get_speed_bonus()) +"x"
        speed_display = self.hud_smallfont.render(speed_text, True, (255, 255, 255))
        speed_x = screen.get_width() - (text_padding + speed_display.get_width())
        speed_y = score_y - (speed_display.get_height() + text_padding / 2)
        screen.blit(speed_display, (speed_x, speed_y))
        
    def update(self):
        self.ball.update(self)
        
        if self.lives < 1:
            self.closed = True
        
    def handle_input_event(self, event):
        self.player.handle_input_event(event)
        
        pygame.mouse.set_visible(False)
        
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self.closed = True